package com.dudu.util;

import com.dudu.exception.ErrorCode;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dudu on 16/8/5.
 */
public class ResponseBuilder {

    private PageDto pageDto;

    private Map<String, Object> data;

    private Object dataObj;

    private ErrorCode errorCode;

    private String errorMsg;

    private ResponseBuilder() {
    }

    public static ResponseBuilder create() {
        return new ResponseBuilder();
    }

    public static ResponseBuilder create(ErrorCode errorCode) {
        ResponseBuilder builder = new ResponseBuilder();
        builder.setErrorCode(errorCode);
        return builder;
    }

    public static ResponseBuilder create(ErrorCode errorCode, String errorMsg) {
        ResponseBuilder builder = new ResponseBuilder();
        builder.setErrorCode(errorCode, errorMsg);
        return builder;
    }

    public static ResponseBuilder create(Map<String, Object> data, PageDto pageDto) {
        ResponseBuilder builder = new ResponseBuilder();
        builder.setData(data);
        builder.setPageDto(pageDto);
        return builder;
    }

    public static ResponseBuilder create(Object data, PageDto pageDto) {
        ResponseBuilder builder = new ResponseBuilder();
        if (data != null) {
            for (Field field : data.getClass().getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    Object value = field.get(data);
                    if (value != null) {
                        builder.addData(field.getName(), value);
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        builder.setPageDto(pageDto);
        return builder;
    }

    public static ResponseBuilder create(Object data) {
        ResponseBuilder builder = new ResponseBuilder();
        if (data != null) {
            builder.setData(data);
        }
        return builder;
    }

    public ResponseBuilder setPageDto(PageDto pageDto) {
        this.pageDto = pageDto;
        return this;
    }

    public ResponseBuilder setData(Map<String, Object> data) {
        this.data = data;
        return this;
    }

    public ResponseBuilder setData(Object obj) {
        this.dataObj = obj;
        return this;
    }

    public ResponseBuilder addData(String key, Object value) {
        Preconditions.checkArgument(StringUtils.isNoneBlank(key), "data key can not be empty");
        if (this.data == null) {
            this.data = new HashMap<String, Object>();
        }
        this.data.put(key, value);
        return this;
    }

    public ResponseBuilder setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
        return this;
    }

    public ResponseBuilder setErrorCode(ErrorCode errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        return this;
    }

    public WebResponse<?> build() {
        WebResponse<Object> result = null;
        if (this.errorCode != null) {
            result = new WebResponse<Object>(errorCode);
        } else {
            result = new WebResponse<Object>();
        }
        if (dataObj != null) {
            result.setData(dataObj);
        } else {
            result.setData(data);
        }
        result.setPageDto(pageDto);
        if (StringUtils.isNoneBlank(errorMsg)) {
            result.setMsg(errorMsg);
        }
        return result;
    }
}
